home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / lib / init / usplash-fsck-functions.sh < prev    next >
Text File  |  2008-10-14  |  6KB  |  170 lines

  1. #
  2. # Functions for reporting fsck progress in usplash
  3. #
  4. # (C) 2008 Canonical Ltd.
  5. # Author: Martin Pitt <martin.pitt@ubuntu.com>
  6. #
  7.  
  8. # convert a "pass cur max" progress triple from fsck to a progress percentage
  9. # based on calc_percent() from e2fsck
  10. fsck_progress_to_percent() {
  11.     if [ $1 = 1 ]; then
  12.         PERCENT=$(($2 * 70 / $3))
  13.     elif [ $1 = 2 ]; then
  14.         PERCENT=$(($2 * 20 / $3 + 70))
  15.     elif [ $1 = 3 ]; then
  16.         PERCENT=$(($2 * 2 / $3 + 90))
  17.     elif [ $1 = 4 ]; then
  18.         PERCENT=$(($2 * 3 / $3 + 92))
  19.     elif [ $1 = 5 ]; then
  20.         PERCENT=$(($2 * 5 / $3 + 95))
  21.     else
  22.         PERCENT=100
  23.     fi
  24. }
  25.  
  26. # read current fsck status ($PASS, $CUR, $MAX) from file descriptor 4
  27. # this assumes that fsck was started in the background ($!)
  28. get_fsck_status()
  29. {
  30.         local a b c S
  31.  
  32.         unset a
  33.         # only consider the last line
  34.         while true; do
  35.             PASS=$a
  36.             CUR=$b
  37.             MAX=$c
  38.             read a b c rest <&4
  39.             if [ -n "$PASS" ] && [ -z "$a" ]; then
  40.                 break;
  41.             fi
  42.  
  43.             # if we did not read anything, check if the process is still
  44.             # actually running, or just waiting to be reaped
  45.             if [ -z "$PASS" ] && [ -z "$a" ]; then
  46.                 S=`ps -o state --no-headers -p $!` || break
  47.                 [ "$S" != "Z" ] || break
  48.                 # do not spin while waiting for fsck to start up
  49.                 sleep 0.1
  50.             fi
  51.         done
  52. }
  53.  
  54. # Set $NAME to a human readable description of which partitions are currently
  55. # being checked. Set $CLEAN if this is only a routine check on clean
  56. # partitions which can be skipped.
  57. get_checked_names ()
  58. {
  59.         local DEVS DUMP LABEL
  60.  
  61.         FSCKPROCS=$(ps --no-headers -C 'fsck.ext2 fsck.ext3' -o pid,args | grep /dev)
  62.         DEVS=$(echo "$FSCKPROCS" | sed 's_^.*\(/dev/[^[:space:]]*\).*$_\1_')
  63.         FSCKPIDS=$(echo "$FSCKPROCS" | sed 's_^[[:space:]]*\([[:digit:]]\+\).*$_\1_')
  64.  
  65.         if [ -z "$DEVS" ]; then
  66.             unset NAME
  67.             return 0
  68.         fi
  69.  
  70.         CLEAN=1
  71.         unset NAME
  72.         for DEV in $DEVS; do
  73.             DUMP=$(dumpe2fs -h $DEV)
  74.             if ! echo "$DUMP" | grep -q 'state:[[:space:]]*clean$'; then
  75.                 unset CLEAN
  76.             fi
  77.  
  78.             LABEL=$(vol_id --label $DEV)
  79.             [ -z "$NAME" ] || NAME="$NAME, "
  80.             if [ -n "$LABEL" ]; then
  81.                 NAME="$NAME$LABEL ($DEV)"
  82.             else
  83.                 NAME="$NAME$DEV"
  84.             fi
  85.         done
  86. }
  87.  
  88. # Read fsck progress from file $1 and display progress in usplash.
  89. usplash_progress() {
  90.         exec 4<$1
  91.         unset CANCEL
  92.         ESCAPE=`/bin/echo -ne "\x1B"`
  93.         FIRST=1
  94.         PREVPERCENT=0
  95.  
  96.         while true; do
  97.             sleep 0.5
  98.             get_fsck_status
  99.             [ -n "$PASS" ] || break
  100.  
  101.             fsck_progress_to_percent "$PASS" "$CUR" "$MAX"
  102.  
  103.             # check if fsck advanced to the next drive
  104.             if [ "$PREVPERCENT" -gt "$PERCENT" ]; then
  105.                 if [ -n "$CANCEL" ]; then
  106.                     usplash_write "STATUS skip                                                        "
  107.                 else
  108.                     usplash_write "STATUS                                                             "
  109.                 fi
  110.                 FIRST=1
  111.             fi
  112.             PREVPERCENT=$PERCENT
  113.  
  114.             # lazy initialization of output and progress report on the first
  115.             # progress line that we receive; this avoids starting the output
  116.             # for clean or non-ext[23] partitions
  117.             if [ -n "$FIRST" ]; then
  118.                 usplash_write "TIMEOUT 0"
  119.  
  120.                 # show which device is being checked
  121.                 get_checked_names
  122.                 [ -n "$NAME" ] || break
  123.  
  124.                 usplash_write "VERBOSE on"
  125.                 if [ "$CLEAN" ]; then
  126.                     usplash_write "TEXT Routine check of drives: $NAME..."
  127.                     usplash_write "TEXT Press ESC to skip"
  128.                 else
  129.                     usplash_write "TEXT Unclean shutdown, checking drives:"
  130.                     usplash_write "TEXT $NAME..."
  131.                 fi
  132.  
  133.                 unset FIRST
  134.             fi
  135.  
  136.             usplash_write "STATUS $PERCENT% (stage $PASS/5, $CUR/$MAX)                       "
  137.             echo "Checking drive $NAME: $PERCENT% (stage $PASS/5, $CUR/$MAX)" >/dev/console
  138.  
  139.             # ESC interrupts check for clean drives
  140.             if [ -n "$CLEAN" ]; then
  141.                 if FAIL_NO_USPLASH=1 usplash_write "INPUTCHAR"; then
  142.                     read ch < /dev/.initramfs/usplash_outfifo
  143.                     if [ "$ch" = "$ESCAPE" ]; then
  144.                         kill $FSCKPIDS
  145.                         CANCEL=1
  146.                         continue # there might be more drives, so do not break
  147.                     fi
  148.                 fi
  149.             fi
  150.         done
  151.  
  152.         if [ -n "$CANCEL" ]; then
  153.             usplash_write "STATUS skip                                                        "
  154.         else
  155.             usplash_write "STATUS                                                             "
  156.         fi
  157.         usplash_write "VERBOSE default"
  158.         usplash_write "TEXT Drive checks finished."
  159.         usplash_write "TIMEOUT 15"
  160.         wait %1 # to collect fsck's exit code
  161.         EXITCODE=$?
  162.         [ -n "$CANCEL" ] && FSCKCODE=0 || FSCKCODE=$EXITCODE
  163.         if [ "$FSCKCODE" -gt 1 ]; then
  164.             # non-correctable failure which requires sulogin: quit usplash and
  165.             # restore stdin/out/err
  166.             usplash_write "QUIT"
  167.             exec </dev/console >/dev/console 2>/dev/console
  168.         fi
  169. }
  170.